const maximalSquare = (matrix) => {
  let maxSideLength = 0; // 相当于记录保持者
  const dp = new Array(matrix.length);
  for (let i = 0; i < matrix.length; i++) {
    dp[i] = new Array(matrix[0].length).fill(0);
    for (let j = 0; j < matrix[0].length; j++) {
      if (matrix[i][j] === "1") {
        if (i === 0 || j === 0) {
          // base case
          dp[i][j] = 1; // 第一行和第一列的 dp 值只能为 1
        } else {
          dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1;
        }
        maxSideLength = Math.max(maxSideLength, dp[i][j]);
      }
    }
  }
  return maxSideLength * maxSideLength;
};
